#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QFileDialog>
#include <QSettings>
#include <QDateTime>
#include <QDebug>
#include <QScrollArea>
#include <QTimer>
#include <chrono>

#include "jointimage.h"
#include "splitimgtopt.h"
#include "rotateimg.h"
#include "mergeimg.h"
#include "myshowimg.h"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}
Widget::~Widget()
{
    delete ui;
}
void Widget::splitSixImgOne()
{
    //QStringList filePath_list = QFileDialog::getOpenFileNames(this, tr("选择图像"), "E:\\jointImg\\src2Pic",
    // tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
    //if(filePath_list.isEmpty()) {
    // return;
    //}
    QStringList filePath_list;
    filePath_list << "E:\\jointImg\\src2Pic\\01.jpg" << "E:\\jointImg\\src2Pic\\02.jpg" << "E:\\jointImg\\src2Pic\\03.jpg" <<
                     "E:\\jointImg\\src2Pic\\04.jpg" << "E:\\jointImg\\src2Pic\\05.jpg" << "E:\\jointImg\\src2Pic\\06.jpg";
    jointImage *pJointImage;
    pJointImage = new jointImage();
    QString dstPath = "E:/jointImg/src2Pic";
    QFileInfo info;
    for(int i = 0; i < filePath_list.size(); ++i) {
        info = QFileInfo(filePath_list[i]);
        std::string filename = info.baseName().toLocal8Bit().toStdString();
        pJointImage->getTowerLines(dstPath, filePath_list[i], filename);
        std::string szTowerPicPath = "E:\\jointImg\\src2Pic\\" + filename + "canny.jpg";
        pJointImage->getPicTowerPart(szTowerPicPath, filename);
    }
    filePath_list.clear();
    keypoints.jointTreePoints = pJointImage->getTreePoints();
    delete pJointImage;
}
void Widget::splitSixImgTwo()
{

    QStringList filePath_list;
    filePath_list << "E:\\jointImg\\src2Pic\\splitSP\\01transformSP.jpg" << "E:\\jointImg\\src2Pic\\splitSP\\02transformSP.jpg" << "E:\\jointImg\\src2Pic\\splitSP\\03transformSP.jpg" <<
                     "E:\\jointImg\\src2Pic\\splitSP\\04transformSP.jpg" << "E:\\jointImg\\src2Pic\\splitSP\\05transformSP.jpg" << "E:\\jointImg\\src2Pic\\splitSP\\06transformSP.jpg";
    splitImgToPt *pSplitImgToPt = new splitImgToPt(686, 1130);
    std::vector<std::pair<int, int>> LandRAngles = {
        { 120, 60 },
        { 120, 57 },
        { 120, 48 },
        { 120, 55 },
        { 120, 55 },
        { 120, 55 },
    };
    for (int i = 0; i < filePath_list.size(); ++i) {
        pSplitImgToPt->setTwoLine(keypoints.jointTreePoints[i]);
        std::string path = filePath_list[i].toLocal8Bit().toStdString();
        pSplitImgToPt->splitImg(path, LandRAngles[i].first, LandRAngles[i].second);
    }
    filePath_list.clear();
    keypoints.splitFivePoint = pSplitImgToPt->getFivePoint();
    delete pSplitImgToPt;
}

void Widget::rotateSixImg()
{
    //QStringList filePath_list = QFileDialog::getOpenFileNames(this, tr("选择图像"), "E:\\jointImg\\src2Pic\\split-lastArr",
    //tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
    //if (filePath_list.isEmpty()) {
    //return;
    //}
    QStringList filePath_list;
    filePath_list << "E:\\jointImg\\src2Pic\\split-lastArr\\split-last1.jpg" << "E:\\jointImg\\src2Pic\\split-lastArr\\split-last2.jpg" <<
                     "E:\\jointImg\\src2Pic\\split-lastArr\\split-last3.jpg" << "E:\\jointImg\\src2Pic\\split-lastArr\\split-last4.jpg" <<
                     "E:\\jointImg\\src2Pic\\split-lastArr\\split-last5.jpg" << "E:\\jointImg\\src2Pic\\split-lastArr\\split-last6.jpg";
    RotateImg *rotateimg = new RotateImg(keypoints.splitFivePoint);
    for (int i = 0; i < filePath_list.size(); ++i) {
        std::string path = filePath_list[i].toLocal8Bit().toStdString();
        rotateimg->transFormImg(path, i * 60.0);
    }
    filePath_list.clear();
    keypoints.rotatedpointArr = rotateimg->getFivePoint();
    delete rotateimg;
}
void Widget::mergeSixImg()
{
    //    QStringList filePath_list = QFileDialog::getOpenFileNames(this, tr("选择图像"), "E:\\jointImg\\src2Pic\\RotationImg",
    //                                tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
    //    if (filePath_list.isEmpty()) {
    //        return;
    //    }

    QStringList filePath_list;
    filePath_list << "E:\\jointImg\\src2Pic\\rotationImg\\rotation-1.jpg" << "E:\\jointImg\\src2Pic\\rotationImg\\rotation-2.jpg" <<
                     "E:\\jointImg\\src2Pic\\rotationImg\\rotation-3.jpg" << "E:\\jointImg\\src2Pic\\rotationImg\\rotation-4.jpg" <<
                     "E:\\jointImg\\src2Pic\\rotationImg\\rotation-5.jpg" << "E:\\jointImg\\src2Pic\\rotationImg\\rotation-6.jpg";

    std::string basePath = filePath_list[0].toLocal8Bit().toStdString();
    MergeImg *merger = new MergeImg(basePath, keypoints.rotatedpointArr);
    for (int i = 1; i < filePath_list.size(); ++i) {
        std::string path = filePath_list[i].toLocal8Bit().toStdString();
        merger->Merge(path, i);
        cout << "merge " << i << "success" << endl;
    }
    cout << "merge over!!!" << endl;
    filePath_list.clear();
    delete merger;
}
void Widget::on_MergeResult_clicked()
{
    // 记录开始时间
    auto start = std::chrono::high_resolution_clock::now();
    splitSixImgOne();
    splitSixImgTwo();
    rotateSixImg();
    mergeSixImg();
    // 记录结束时间
    auto end = std::chrono::high_resolution_clock::now();

    // 计算运行时间
    auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    qDebug() << "运行时间:" << duration.count() << " 毫秒";

    cv::Mat src = cv::imread("E:\\jointImg\\src2Pic\\resultDir\\result-5.jpg");
    putText(src, "1", Point(1000, 350), cv::FONT_HERSHEY_SIMPLEX, 10.0, Scalar(0, 0, 255), 5);
    putText(src, "2", Point(100, 841), cv::FONT_HERSHEY_SIMPLEX, 10.0, Scalar(0, 0, 255), 5);
    putText(src, "3", Point(100, 1997), cv::FONT_HERSHEY_SIMPLEX, 10.0, Scalar(0, 0, 255), 5);
    putText(src, "4", Point(1000, 2720), cv::FONT_HERSHEY_SIMPLEX, 10.0, Scalar(0, 0, 255), 5);
    putText(src, "5", Point(2500, 2267), cv::FONT_HERSHEY_SIMPLEX, 10.0, Scalar(0, 0, 255), 5);
    putText(src, "6", Point(2500, 950), cv::FONT_HERSHEY_SIMPLEX, 10.0, Scalar(0, 0, 255), 5);
    Mat dst;
    cv::resize(src,dst,Size(900,828));
    imwrite("E:\\jointImg\\src2Pic\\resultDir\\result-55.jpg",dst);
}
void Widget::on_IshowImgBtn_clicked()
{
//    QEventLoop loop;
//    MyshowImg sw;
//    sw.show();
//    loop.exec();

    MyshowImg* sw=new MyshowImg;
    sw->show();
}


void Widget::on_splitBtn_clicked()
{

    QString filename;
    //打开一个文件
    filename = QFileDialog::getOpenFileName(this, tr("选择图像"), "E:\\moreimgmerge\\jointImg\\src2Pic",
                                            tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
    if(filename.isEmpty()) {
        return;
    }
    jointImage *pJointImage;
    pJointImage = new jointImage();
    QString dstPath = "E:/jointImg/src2Pic";
    QFileInfo info;
    info = QFileInfo(filename);
    std::string filenamesp=info.baseName().toLocal8Bit().toStdString();
    pJointImage->getTowerLines(dstPath, filename,filenamesp);
    std::string szTowerPicPath = "E:\\jointImg\\src2Pic\\"+filenamesp+"canny.jpg";
    pJointImage->getPicTowerPart(szTowerPicPath,filenamesp);
    delete pJointImage;

}



